import {
    selectFontList,
    getCssData,
} from "../../common/js/practice_set.js";

let app = getApp(),
    that = null,
    helper = app.globalData.helper;
export default {
    data() {
        return {
            CustomBar: this.CustomBar,
            listData: [],
            backgroundData: [],
            title: '',
            id: 0,
            openSetModal: false, // 开启设置模态框
            openAiExplanationModal: false, // 开启AI解析模态框
            aiExplanationContent: '',
            mainType: 0,
            maxPage: 0,
            questionType: 0,
            isContinue: 2,
            currentPage: 1,
            currentAction: 0,
            selectModel: ['刷题模式', '背题模式'],
            selectModelIndex: 0,
            curretnAnwer: '',
            options: ['A', 'B', 'C', 'D', 'E', 'F'],
            isLoad: false,
            endId: 0,
            limit: 10,
            swiperCurrent: 0,
            doExamList: [],
            isCollection: 0,
            isAutoNext: false,
            isNightMode: false,
            isDarkMode: false,
            isAutoCorrectNext: false,
            isShowAutoNextDialog: true,
            extend_id: '',
            pageSelectList: '',
            selectPage: '',
            questionFont: 1,
            questionCount: 0,
            selectFontList: selectFontList,
            moreActionNavIndex: 0,
            moreActionNavList: ['题目操作', '答题设置', '显示设置'],
            answerSheetStatus: [],
            cssData: {},
            openAnswerCardModal: false, // 答题卡弹窗显示控制
        };
    },
    onLoad(options) {
        that = this;
        that.id = options.id;
        that.title = options.title;
        that.extend_id = helper.variableDefalut(options.extend_id, 0);
        that.mainType = options.mainType;
        that.maxPage = options.max_page || 0;
        that.questionType = options.question_type || 0;
        that.questionCount = options.question_count || 0;
        that.isAutoCorrectNext = Boolean(uni.getStorageSync(app.globalData.config.storage.practiceCorrectNextKey));
        that.isAutoNext = Boolean(uni.getStorageSync(app.globalData.config.storage.practiceAllNextKey));
        that.isNightMode = Boolean(uni.getStorageSync(app.globalData.config.storage.practiceIsNightKey));
        that.isDarkMode = Boolean(uni.getStorageSync(app.globalData.config.storage.practiceIsNightKey));
        that.isShowAutoNextDialog = uni.getStorageSync(app.globalData.config.storage.practiceNextDialogKey);
        app.globalData.showShareMenu();
        that.initAnswerSheet();
        that.getContinueData();
    },
    onShow() {
        that.initCss();
    },
    onShareAppMessage() {
        let item = that.listData[that.swiperCurrent];
        let shareConfig = app.globalData.getShareConfig();
        shareConfig.title = item.questionAsk;
        shareConfig.path = '/pages/practice/question/detail?id=' + item.id;
        return shareConfig;
    },
    methods: {
        // 初始化答题卡方法
        initAnswerSheet() {
            // 初始化答题卡逻辑
            let answerSheetStatus = [];
            let limit = that.limit || 10; // 每页题目数量，默认10
            
            for (let i = 0; i < that.questionCount; i++) {
                // 计算当前题目在第几页
                let questionPage = Math.floor(i / limit) + 1;
                // 计算当前题目在当前页的索引（从1开始）
                let questionIndex = (i % limit) + 1;
                
                answerSheetStatus.push({ 
                    index: i + 1,
                    questionPage: questionPage,
                    questionIndex: questionIndex,
                    status: 'unanswered' // 默认未作答
                });
            }
            that.answerSheetStatus = answerSheetStatus;
        },
        
        // 更新答题卡状态
        updateAnswerCardStatus() {
            if (!that.listData || that.listData.length === 0) return;
            
            // 获取当前页的题目列表
            let currentPageQuestions = that.listData;
            let startIndex = (that.currentPage - 1) * that.limit;
            
            // 遍历当前页的题目，更新答题卡状态
            for (let i = 0; i < currentPageQuestions.length; i++) {
                let question = currentPageQuestions[i];
                let answerSheetIndex = startIndex + i;
                
                // 确保索引在有效范围内
                if (answerSheetIndex < that.answerSheetStatus.length) {
                    // 根据题目的作答状态更新答题卡状态
                    if (question.is_correct === 1) {
                        that.answerSheetStatus[answerSheetIndex].status = 'correct';
                    } else if (question.is_correct === 2) {
                        that.answerSheetStatus[answerSheetIndex].status = 'incorrect';
                    } else if (question.select_answer && question.select_answer.length > 0) {
                        // 已作答但未判断正误
                        that.answerSheetStatus[answerSheetIndex].status = 'answered';
                    } else {
                        that.answerSheetStatus[answerSheetIndex].status = 'unanswered';
                    }
                }
            }
        },
        onLongPress(e) {
            uni.setClipboardData({
                data: e,
                success() {
                    app.showToast('复制成功');
                },
                fail(res) {
                    console.log(res);
                }
            });
        },
        onCleanQuestionTap() {
            uni.showModal({
                title: '提示',
                content: '确认清空答题记录',
                showCancel: true,
                success: (t) => {
                    if (t.confirm) {
                        that.clearAnswer(0);
                        that.openSetModal = false;
                    }
                }
            });
        },
        onCleanErrorQuestionTap() {
            uni.showModal({
                title: '提示',
                content: '确认清空错题记录',
                showCancel: true,
                success: (t) => {
                    if (t.confirm) {
                        that.clearAnswer(2);
                        that.openSetModal = false;
                    }
                }
            });
        },
        moreActionIndexTap(options) {
            that.moreActionNavIndex = options.currentTarget.dataset.index
        },
        initCss() {
            let isDarkMode = that.isDarkMode;
            let questionFont = uni.getStorageSync(app.globalData.config.storage.practiceQuestionFontKey);
            questionFont = questionFont == '' ? 1 : questionFont;
            that.questionFont = questionFont;
            that.cssData = getCssData(questionFont, isDarkMode);
        },
        selectModelTap(options) {
            that.selectModelIndex = options.currentTarget.dataset.id;
            that.listData = that.formatQuestionList(that.listData, false);
        },
        switchQuestionTap(options) {
            console.log(options.currentTarget.dataset.type)
            let current = options.detail.current;
            that.swiperCurrent = current + 1;
            that.refreshSomething();
        },
        transitionTap(options) {
            let dx = options.detail.dx;
            let action = dx > 30 ? 2 : dx < -30 ? 1 : 0;
            let maxPage = that.maxPage;
            let currentPage = that.currentPage;
            let swiperCurrent = that.swiperCurrent;
            let isLoad = that.isLoad;
            let listDataLength = that.listData.length;
            if (action === 0) {
                return;
            }
            if (isLoad === true) {
                return;
            }
            let canLoad = (action == 1 && swiperCurrent == 0) || (action == 2 && swiperCurrent == listDataLength -
                1);
            if (!canLoad) {
                return;
            }
            let donLoad = (action === 1 && currentPage == 1) || (action == 2 && that.currentPage == maxPage);
            if (donLoad) {
                return;
            }
            that.currentAction = action;
        },
        animationfinishTap(options) {
            if ('touch' == options.detail.source) {
                let current = options.detail.current;
                let currentPage = that.currentPage;
                let currentAction = that.currentAction;
                that.swiperCurrent = current;
                
                if (current == that.listData.length - 1 && currentPage == that.maxPage) {
                    app.showToast('最后一道题啦');
                }
                if (currentAction > 0) {
                    that.currentPage = currentAction === 1 ? currentPage - 1 : currentPage + 1;
                    that.getQuestion(currentAction);
                }
                that.refreshSomething();
            }
        },
        setQuestionDirectionTap(current) {
            let nextCurrentPage = that.currentPage;
            let nextSwiperCurrent = that.swiperCurrent;
            if (current == 1) {
                if (nextSwiperCurrent == 0) {
                    nextCurrentPage--;
                    if (nextCurrentPage == 0) {
                        app.showToast('没有上一题啦');
                        return;
                    }
                    that.currentPage = nextCurrentPage;
                    that.getQuestion(1);
                } else {
                    that.swiperCurrent = nextSwiperCurrent - 1;
                }
            } else {
                if (nextSwiperCurrent == that.listData.length - 1) {
                    nextCurrentPage++;
                    if (nextCurrentPage > that.maxPage) {
                        app.showToast('没有下一题啦');
                        return;
                    }
                    that.currentPage = nextCurrentPage;
                    that.getQuestion(2);
                } else {
                    that.swiperCurrent = nextSwiperCurrent + 1;
                }
            }
            that.refreshSomething();
        },
        getContinueData() {
            let e = that;
            app.globalData.server
                .postRequest('question/is_continue', {
                    id: e.id,
                    type: e.mainType,
                    extend_id: e.extend_id,
                    question_type: e.questionType
                })
                .then(function (t) {
                    let e = t.data;

                    if (1 == e.is_continue) {
                        uni.showModal({
                            title: '提示',
                            content: '检测到上次做题记录，是否继续答题',
                            showCancel: true,
                            cancelText: '重新答题',
                            confirmText: '继续答题',
                            success: (t) => {
                                if (t.confirm) {
                                    that.currentPage = e.last_question_page;
                                    that.getQuestion(2, e.last_question_id);
                                } else {
                                    if (that.mainType == 2 || that.mainType == 3) {
                                        //收藏和错题不需要清空
                                        that.getQuestion(2, 0);
                                    } else {
                                        that.clearAnswer();
                                    }
                                }
                            }
                        });
                    } else {
                        that.getQuestion(2, 0);
                    }
                })
                .catch((a) => {
                    console.log(a);
                    app.showToast('获取答题记录失败');
                });
        },
        clearAnswer(status) {
            app.globalData.server
                .postRequest('question/clear', {
                    id: that.id,
                    status: status,
                    type: that.mainType,
                    extend_id: that.extend_id,
                    question_type: that.questionType
                })
                .then(function (response) {
                    that.getQuestion(2, 0);
                })
                .catch(function (err) {
                    console.log(err);
                });
        },

        getQuestion(type = 2, jumpToId = 0, jumpToIndex = null) {
            //标记加载中
            that.isLoad = true;
            that.currentAction = 0;

            let data = that;
            let limitTime = 3000;
            app.globalData.server
                .getRequest('v3/get_topic', {
                    id: data.id,
                    type: data.mainType,
                    extend_id: data.extend_id,
                    question_type: data.questionType,
                    page: data.currentPage || 1,
                    limit: data.limit
                })
                .then((res) => {
                    let list = that.formatQuestionList(res.data.question_list, true);
                    let background = res.data.question_background;
                    that.backgroundData = background;
                    
                    if (list.length > 0) {
                        that.listData = list;
                        
                        // 设置默认的swiperCurrent
                        if (jumpToIndex !== null && jumpToIndex >= 0 && jumpToIndex < list.length) {
                            // 如果指定了jumpToIndex，直接跳转到该索引位置
                            that.swiperCurrent = jumpToIndex;
                        } else {
                            // 否则使用默认的索引位置
                            let newSwiperCurrent = type == 2 ? 0 : that.listData.length - 1;
                            that.swiperCurrent = newSwiperCurrent;
                        }
                    }

                    // 如果有指定跳转题目ID，则尝试跳转到该题目
                    if (jumpToId > 0 && jumpToIndex === null) {
                        const targetIndex = that.listData.findIndex(item => item && item.id === jumpToId);
                        if (targetIndex !== -1) {
                            that.swiperCurrent = targetIndex;
                            app.showToast('已跳转至上次做题位置');
                        }
                    }

                    that.refreshSomething();
                })
                .catch((res) => {
                    console.log(res);
                })
                .finally(function () {
                    //保存答题记录
                    that.uploadDoCache(1); //标记数据加载完成
                    setTimeout(function () {
                        that.isLoad = false;
                    }, limitTime);
                });
        },

        formatQuestionList(list, isFirst) {
            for (var i = 0; i < list.length; i++) {
                let type = list[i]['type']
                if (type < 4) {
                    let correctOption = list[i]['correctOption'];
                    if (that.selectModelIndex == 1) {
                        // 背题模式
                        list[i]['select_answer'] = correctOption;
                        list[i]['correctOptionArr'] = app.globalData.practice.buildAnswerOption(correctOption)
                        list[i]['is_correct'] = 1;
                    } else {
                        // 刷题模式
                        if (!isFirst) {
                            list[i]['select_answer'] = "";
                            list[i]['is_correct'] = 0;
                            list[i]['correctOptionArr'] = app.globalData.practice.buildAnswerOption(correctOption)
                        }
                    }
                }
            }
            return list;
        },

        checkDoCache(e) {
            that.doExamList.push(e);

            if (app.globalData.isLogin && this.doExamList.length >= 30) {
                that.uploadDoCache();
            }
        },

        /**
         *  保存答案
         * @param {*} action  1.普通保存 2.返回保存 3.交卷保存
         */
        uploadDoCache(action) {
            if (0 != this.doExamList.length) {
                app.globalData.server
                    .postRequest('question/save_answer', {
                        course_id: that.id,
                        source: that.mainType,
                        answer_data: that.getCanUploadData()
                    })
                    .then(function (t) {
                        that.doExamList = [];
                        app.showToast('保存成功');

                        if (action == 3) {
                            that.toResultPage();
                        }
                    })
                    .catch(function (e) {
                        console.log(e);
                        app.showToast('答题失败');
                    });
            } else {
                if (action == 3) {
                    that.toResultPage();
                }
            }
        },

        getCanUploadData() {
            for (var t = new Array(), a = this.doExamList, e = 0; e < a.length; e++) {
                let i = a[e];

                if (i.select_answer) {
                    if (2 == i.type) {
                        let o = {
                            course_id: i.course_id,
                            extend_id: i.extend_id,
                            question_id: i.id,
                            question_type: i.type,
                            question_page: i.page,
                            question_answer: i.select_answer.join(''),
                            question_correct: i.is_correct
                        };
                        t.push(o);
                    } else {
                        let n = {
                            course_id: i.course_id,
                            extend_id: i.extend_id,
                            question_id: i.id,
                            question_type: i.type,
                            question_page: i.page,
                            question_answer: i.select_answer,
                            question_correct: i.is_correct
                        };
                        t.push(n);
                    }
                }
            }

            return t;
        },

        refreshSomething() {
            that.getQuestionTopicType();
            that.getMultiCorrectOption();
            let swiperCurrent = that.swiperCurrent;

            if (that.listData[swiperCurrent] != null) {
                that.isCollection = that.listData[swiperCurrent].isCollection || 0;
            }
            
            // 更新答题卡状态
            that.updateAnswerCardStatus();
        },

        getQuestionTopicType() {
            let swiperCurrent = that.swiperCurrent;
            console.log(8888);
            console.log(swiperCurrent);
            if (that.listData[swiperCurrent] != null) {
                let e = that.listData[swiperCurrent].type;
                console.log(that.listData[swiperCurrent]);
                that.curretnAnwer = app.globalData.practice.checkAnswer(e);
            }
        },

        singleSelectTap(event) {
            let index = that.swiperCurrent;

            if (!that.listData.hasOwnProperty(index)) {
                return;
            }

            let currentQuestion = that.listData[index];

            if (currentQuestion.is_correct == 0) {
                let selectedAnswer = event.currentTarget.dataset.answer;
                let correctStatus = 0;
                if (selectedAnswer === currentQuestion.correctOption) {
                    correctStatus = 1;
                } else {
                    correctStatus = 2;
                }

                currentQuestion.is_correct = correctStatus;
                currentQuestion.select_answer = selectedAnswer;
                let updatedListData = that.listData;
                updatedListData[that.swiperCurrent] = currentQuestion;
                that.listData = updatedListData;
                
                // 更新答题卡状态
                that.updateAnswerCardStatus();
                
                that.checkDoCache(currentQuestion);
                that.skipNextQuestion(correctStatus);
            }
        },

        multipleSelectTap(event) {
            let index = that.swiperCurrent;
            if (!that.listData.hasOwnProperty(index)) {
                return;
            }
            let currentQuestion = that.listData[index];
            if (currentQuestion.is_correct == 0) {
                let optionIndex = event.currentTarget.dataset.index;
                let optionValue = that.options[optionIndex];
                if (currentQuestion.select_answer) {
                    if (currentQuestion.select_answer[optionIndex] == optionValue) {
                        currentQuestion.select_answer[optionIndex] = '';
                    } else {
                        currentQuestion.select_answer[optionIndex] = optionValue;
                    }
                } else {
                    let newAnswerArray = new Array(6);
                    newAnswerArray[optionIndex] = optionValue;
                    currentQuestion.select_answer = newAnswerArray;
                }
                that.$set(that.listData, that.swiperCurrent, currentQuestion);
            }
        },

        multipleSelectCommitTap(event) {
            let index = that.swiperCurrent;
            if (!that.listData.hasOwnProperty(index)) {
                return;
            }

            let currentQuestion = that.listData[index];

            if (currentQuestion.is_correct == 0) {
                let selectedAnswer = currentQuestion.select_answer || [];
                let correctOptionArr = currentQuestion.correctOptionArr || [];
                let correctStatus = 0;

                if (selectedAnswer.length == 0) {
                    app.showToast('请选择答案');
                    return;
                }

                if (selectedAnswer.length != correctOptionArr.length) {
                    correctStatus = 2;
                } else {
                    let isAllCorrect = true;
                    for (let i = 0; i < selectedAnswer.length; i++) {
                        if (correctOptionArr.indexOf(selectedAnswer[i]) == -1) {
                            isAllCorrect = false;
                            break;
                        }
                    }
                    correctStatus = isAllCorrect ? 1 : 2;
                }

                currentQuestion.is_correct = correctStatus;
                let updatedListData = that.listData;
                updatedListData[that.swiperCurrent] = currentQuestion;
                that.listData = updatedListData;
                
                // 更新答题卡状态
                that.updateAnswerCardStatus();
                
                that.checkDoCache(currentQuestion);
                that.skipNextQuestion(correctStatus);
            }
        },

        getMultiCorrectOption() {
            let questionList = that.listData;
            for (var index = 0; index < questionList.length; index++) {
                let question = questionList[index];

                if (question == null) {
                    continue;
                }

                if (question.type == app.globalData.practice.topicType.multi && !question.correctOptionArr) {
                    let correctOptionArray = new Array(6);
                    for (var correctIndex = 0; correctIndex < question.correctOption.length; correctIndex++) {
                        let optionChar = question.correctOption.substr(correctIndex, 1);
                        correctOptionArray['ABCDEF'.indexOf(optionChar)] = optionChar;
                    }

                    question.originSelect = question.select_answer;
                    let selectedOptionArray = new Array(6);

                    if (question.select_answer) {
                        for (let selectIndex = 0; selectIndex < question.select_answer.length; selectIndex++) {
                            let selectedChar = question.select_answer.substr(selectIndex, 1);
                            selectedOptionArray['ABCDEF'.indexOf(selectedChar)] = selectedChar;
                        }
                    }

                    question.correctOptionArr = correctOptionArray;
                    question.select_answer = selectedOptionArray;
                    questionList[index] = question;
                }
            }

            that.listData = questionList;
        },

        skipNextQuestion(correctStatus) {
            if (0 != this.listData.length) {
                let currentIndex = that.swiperCurrent;
                
                // Show dialog if not shown before
                if (!that.isShowAutoNextDialog) {
                    that.isShowAutoNextDialog = true;
                    uni.showModal({
                        title: '提示',
                        content: '可在底部设置是否自动切换下一题',
                        cancelText: '不再提示',
                        confirmText: '知道了',
                        success: function (result) {
                            if (result.cancel) {
                                uni.setStorageSync(app.globalData.config.storage.practiceNextDialogKey,
                                    true);
                            }
                        }
                    });
                }

                // Auto-advance to next question if conditions are met
                if (currentIndex != that.listData.length - 1 && 
                    (that.isAutoNext || (that.isAutoCorrectNext && 1 == correctStatus))) {
                    that.swiperCurrent = currentIndex + 1;
                }

                that.refreshSomething();
            }
        },
        openQuestionImagesTap() {
            let item = that.listData[that.swiperCurrent];
            uni.previewImage({
                urls: item.question_images,
                referrerPolicy: 'origin',
                longPressActions: {
                    itemList: ['发送给朋友', '保存图片', '收藏'],
                    success: function (data) {
                        console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
                    },
                    fail: function (err) {
                        console.log(err.errMsg);
                    }
                }
            });
        },
        onOpenSetTap() {
            that.openSetModal = true;
        },
        onCloseSetTap() {
            that.openSetModal = false;
        },
        onCollectTap() {
            if (0 != that.listData.length) {
                let i = that.listData[that.swiperCurrent];
                if (i.isCollection == 1) {
                    i.isCollection = 2;
                } else {
                    i.isCollection = 1;
                }
                app.globalData.server
                    .postRequest('question/collect', {
                        id: i.id,
                        type: i.isCollection
                    })
                    .then(function (e) {
                        app.showToast(e.message);
                        let o = that.listData;
                        o[that.swiperCurrent] = i;
                        that.isCollection = i.isCollection;
                        that.listData = o;
                    })
                    .catch(function (a) {
                        app.showToast('收藏出错');
                        console.log(a);
                    });
            }
        },

        /**
         * 提交试题
         */
        commitTap() {
            if (that.listData.length == 0) {
                app.showToast('当前题目数据异常');
                return;
            }

            if (that.mainType == app.globalData.practice.mainType.coll) {
                app.showToast('收藏练习不可以交卷');
                return;
            }

            if (that.mainType == app.globalData.practice.mainType.error) {
                app.showToast('错题练习不可以交卷');
                return;
            }

            uni.showModal({
                title: '保存',
                content: '保存本次答题记录~',
                showCancel: true,
                cancelText: '取消',
                confirmText: '确定',
                success: function (action) {
                    if (action.confirm) {
                        that.uploadDoCache(3);
                    }
                }
            });
        },

        toResultPage() {
            let options = that;
            uni.navigateTo({
                url: './result/result?id=' +
                    options.id +
                    '&extend_id=' +
                    options.extend_id +
                    '&mainType=' +
                    options.mainType +
                    '&question_type=' +
                    options.questionType
            });
        },

        navigateBackTap() {
            let canSave =
                that.doExamList.length > 0 && that.mainType != app.globalData.practice.mainType.coll && that
                    .mainType != app.globalData.practice.mainType.error;

            if (canSave) {
                that.uploadDoCache(2);
            }
        },

        /**
         * 题目纠错
         * @param {Object} e
         */
        onFeedbackTap() {
            if (0 != that.listData.length) {
                let i = that.listData[that.swiperCurrent];

                if (0 != that.listData.length) {
                    uni.navigateTo({
                        url: './feedback/feedback?mainType=' + i.mainType + '&id=' + i.id
                    });
                }
            }
        },

        nightModeTap() {
            let isNightMode = !that.isNightMode;
            that.isNightMode = isNightMode;
            that.isDarkMode = isNightMode;
            uni.setStorageSync(app.globalData.config.storage.practiceIsNightKey, isNightMode);
            that.initCss()
        },
        
        toggleDarkMode(e) {
            let isDarkMode = e.detail.value;
            that.isDarkMode = isDarkMode;
            that.isNightMode = isDarkMode;
            uni.setStorageSync(app.globalData.config.storage.practiceIsNightKey, isDarkMode);
            that.initCss()
        },
        autoCorrectNextTap() {
            let isAutoCorrectNext = !that.isAutoCorrectNext;
            that.isAutoCorrectNext = isAutoCorrectNext;
            uni.setStorageSync(app.globalData.config.storage.practiceCorrectNextKey, isAutoCorrectNext);
        },
        autoNextTap() {
            let isAutoNext = !that.isAutoNext;
            that.isAutoNext = isAutoNext;
            uni.setStorageSync(app.globalData.config.storage.practiceAllNextKey, isAutoNext);
        },
        setQuestionFontTap(e) {
            that.questionFont = e.detail.value;
            uni.setStorageSync(app.globalData.config.storage.practiceQuestionFontKey, e.detail.value);
            that.initCss();
        },
        async aiExplanationTap(status) {
            //获取题目信息
            let item = that.listData[that.swiperCurrent];

            //获取题目AI解析
            var aiExplanation = item.aiExplanation;

            //判断是否需要生成ai解析任务
            if (status == true && aiExplanation == '') {
                let res = await app.globalData.service.aiExplanation({
                    id: item.id
                });
                if (res.data.business_code == 1) {
                    //生成中
                    app.showToast(res.message);
                    return;
                } else {
                    // 生成完成
                    aiExplanation = res.data.business_message;
                }
            }

            //清空AI输入内容
            that.aiExplanationContent = '';

            //显示或者关闭模态框
            that.openAiExplanationModal = status;

            //定时任务定时提取文本
            app.startInterval(() => {
                if (aiExplanation.length == 0) {
                    app.stopInterval();
                    return;
                }
                let firstChar = aiExplanation[0];
                aiExplanation = aiExplanation.slice(1);
                that.aiExplanationContent += firstChar;
            }, 50);
        },
        openAnswerCardTap() {
            that.openAnswerCardModal = true;
        },
        closeAnswerCardTap() {
            that.openAnswerCardModal = false;
        },
        // 答题卡按钮点击事件
        answerCardItemTap(item) {
            // 关闭答题卡弹窗
            that.closeAnswerCardTap();
            
            // 如果当前页不是题目所在页，需要先切换到对应页面
            if (that.currentPage !== item.questionPage) {
                that.currentPage = item.questionPage;
                // 调用getQuestion方法获取该页题目，并跳转到指定索引
                that.getQuestion(2, 0, item.questionIndex - 1);
            } else {
                // 如果已经在正确的页面，只需要更新swiperCurrent即可
                if (item.questionIndex - 1 >= 0 && item.questionIndex - 1 < that.listData.length) {
                    that.swiperCurrent = item.questionIndex - 1;
                }
            }
        },
    }
};